<%--
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
--%>
<%@ include file="/taglibs.jsp" %>
<c:set var="title" value="Users" scope="request"/>
<s:layout-render name="/layout/normal.jsp">
  <s:layout-component name="body">
    <ss:secure roles="admin">
      <script type="text/javascript">
        function addUser() {
          var username = prompt('Username');
          if (username) {
            document.add.username.value = username;
            document.add.submit();
          }
        }

        function addRole(uuid, username, role) {
          if (confirm('Really add role \'' + role + '\' to user \'' + username + '\'?')) {
            document.addRoleForm.uuid.value = uuid;
            document.addRoleForm.role.value = role;
            document.addRoleForm.submit();
          }
        }

        function removeRole(uuid, username, role) {
          if (confirm('Really remove role \'' + role + '\' from user \'' + username + '\'?')) {
            document.removeRoleForm.uuid.value = uuid;
            document.removeRoleForm.role.value = role;
            document.removeRoleForm.submit();
          }
        }

        function resetPassword(uuid, username) {
          if (confirm('Really reset password for user \'' + username + '\'?')) {
            document.resetForm.uuid.value = uuid;
            document.resetForm.submit();
          }
        }

        function setEnabled(uuid, username, enabled) {
          if (confirm('Really '+ (enabled ? 'enable' : 'disable') +' user \'' + username + '\'?')) {
            document.setEnabledForm.uuid.value = uuid;
            document.setEnabledForm.enabled.value = enabled;
            document.setEnabledForm.submit();
          }
        }
      </script>
    </ss:secure>

    <c:set var="currentUser" value="${user}" scope="request"/>
    <dt:table name="actionBean.users" id="user" class="data">
      <dt:column title="User">
        <s:link beanclass="org.xptracker.web.DisplayActionBean">
          <s:param name="id" value="${user.id}"/>
          ${user.username}
        </s:link>
      </dt:column>
      <dt:column property="fullName" title="Full Name"/>
      <dt:column property="email" title="Email Address"/>
      <dt:column property="phoneNumber" title="Phone"/>
      <ss:secure roles="admin">
        <dt:column title="">
          <c:if test="${user.username ne currentUser.username}">
            <input type="button" value="${user.enabled ? 'Disable' : 'Enable'}"
                   onclick="setEnabled('${user.id}', '${user.username}', ${!user.enabled})"/>
            <c:if test="${actionBean.authenticator.passwordManager}">
              <input type="button" value="Reset Password" onclick="resetPassword('${user.id}', '${user.username}')"/>
            </c:if>
            <xp:role-list var="role">
              <input type="button" value="${(xp:userHasRole(user, role) ? 'Remove' : 'Add')} ${role.description}"
                     onclick="${(xp:userHasRole(user, role) ? 'removeRole' : 'addRole')}('${user.id}', '${user.username}', '${role}')"/>
            </xp:role-list>
          </c:if>
        </dt:column>
      </ss:secure>
    </dt:table>

    <br/>

    <ss:secure roles="admin">
      <input type="button" value="Add" onclick="addUser()"/>
      <s:form beanclass="org.xptracker.web.UsersActionBean" style="display:inline" name="add">
        <input type="hidden" name="username"/>
        <input type="hidden" name="Add" value="1"/>
      </s:form>
      <s:form beanclass="org.xptracker.web.UsersActionBean" style="display:inline" name="resetForm">
        <input type="hidden" name="uuid"/>
        <input type="hidden" name="ResetPassword" value="1"/>
      </s:form>
      <s:form beanclass="org.xptracker.web.UsersActionBean" style="display:inline" name="addRoleForm">
        <input type="hidden" name="uuid"/>
        <input type="hidden" name="role"/>
        <input type="hidden" name="AddRole"/>
      </s:form>
      <s:form beanclass="org.xptracker.web.UsersActionBean" style="display:inline" name="removeRoleForm">
        <input type="hidden" name="uuid"/>
        <input type="hidden" name="role"/>
        <input type="hidden" name="RemoveRole"/>
      </s:form>
      <s:form beanclass="org.xptracker.web.UsersActionBean" style="display:inline" name="setEnabledForm">
        <input type="hidden" name="uuid"/>
        <input type="hidden" name="enabled"/>
        <input type="hidden" name="SetEnabled"/>
      </s:form>
    </ss:secure>
  </s:layout-component>
</s:layout-render>